package com.gzsxy.sso.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzsxy.sso.common.dto.request.AppRequest;
import com.gzsxy.sso.common.exception.impl.CustomAssert;
import com.gzsxy.sso.common.result.ResultCodeEnum;
import com.gzsxy.sso.core.dao.AppMapper;
import com.gzsxy.sso.core.dao.FunctionMapper;
import com.gzsxy.sso.core.dao.MenuMapper;
import com.gzsxy.sso.core.entity.App;
import com.gzsxy.sso.core.entity.vo.AppQueryVo;
import com.gzsxy.sso.core.service.AppService;
import com.gzsxy.sso.core.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 应用管理
 * @date 2021/11/18 11:29
 */
@Service
public class AppServiceImpl  extends ServiceImpl<AppMapper, App> implements AppService {


    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private FunctionMapper functionMapper;

    /**
     * @description: 查询所有的应用
     * @param
     * @return
     */
    @Override
    public List<App> getAllApp() {
        LambdaQueryWrapper<App> wrapper = Wrappers.lambdaQuery();
        return baseMapper.selectList(wrapper);
    }

    /**
     * @description: 应用分页查询
     * @param
     * @return
     */
    @Override
    public IPage<App> searchPageApp(Page<App> pageParam, AppQueryVo appQueryVo) {
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.orderByDesc("order_id");
        //分页查询
        if (ObjectUtils.isEmpty(appQueryVo)){
           return baseMapper.selectPage(pageParam,queryWrapper);
        }
        //条件查询
        String name = appQueryVo.getName();
        String id = appQueryVo.getId();
        if (StringUtils.isNotEmpty(id)){
            queryWrapper.eq("id",id);
        }
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        return baseMapper.selectPage(pageParam,queryWrapper);
    }


    /**
     * @description: 新增应用
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addApp(AppRequest appRequest) {
        //应用名唯一校验
        ValidateUtil.assertUnique(baseMapper::existCode,appRequest.getName(), ResultCodeEnum.APP_NAME_EXIST.getMessage());
        App app = new App();
        BeanUtils.copyProperties(appRequest,app);

        String pinYinHeadChar = com.gzsxy.sso.common.utils.StringUtils.getPinYinHeadChar(appRequest.getName());
        app.setCode(pinYinHeadChar);
        baseMapper.insert(app);
    }


    /**
     * @description: 编辑应用
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateApp(AppRequest appRequest) {
        App app = baseMapper.selectById(appRequest.getId());
        //应用不存在或已删除
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(app),ResultCodeEnum.APP_NOT_FOUND.getMessage(),ResultCodeEnum.APP_NOT_FOUND.getCode());
        App app1 = new App();
        BeanUtils.copyProperties(appRequest,app1);

        //当修改了应用名称
        if (!StringUtils.equals(app.getName(),appRequest.getName())){
            //应用名唯一校验
            ValidateUtil.assertUnique(baseMapper::existCode,appRequest.getName(), ResultCodeEnum.APP_NAME_EXIST.getMessage());
            String pinYinHeadChar = com.gzsxy.sso.common.utils.StringUtils.getPinYinHeadChar(appRequest.getName());
            app1.setCode(pinYinHeadChar);
        }
        baseMapper.updateById(app1);
    }


    /**
     * @description: 删除应用
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteApp(String appId) {
        //删除失败，存在关联的菜单
        ValidateUtil.isFalse(menuMapper.existByAppId(appId),ResultCodeEnum.APP_DELETE_FAILED_EXIST_MENU.getMessage());
        //删除失败，存在关联的功能
        ValidateUtil.isFalse(functionMapper.existByAppId(appId),ResultCodeEnum.APP_DELETE_FAILED_EXIST_FUNC.getMessage());
        baseMapper.deleteById(appId);
    }

    /**
     * @description: 根据应用id查询
     * @param
     * @return
     */
    @Override
    public App selectById(String appId) {
        return baseMapper.selectById(appId);
    }
}
